package com.sort;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-03-06
 * Time：19:26
 */

//单一类型的比较
public class StudentSort implements Comparable<StudentSort> {

//    继承Comparable 来重新该接口
        public String name;
        public int age;

        public StudentSort(String name,int age) {
            this.name = name;
            this.age=age;
        }

//打印时也需要重写该 toString
    @Override
    public String toString() {
        return "StudentSort{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

//    @Override
//    public int compareTo(StudentSort o) {
//        return this.name.compareTo(o.name);
//    }

    @Override
    public int compareTo(StudentSort o) {
        return o.age-this.age;
    }



}

class Test1 {
    public static void main(String[] args) {
//        统一初始化
//        StudentSort[] students={new StudentSort("游大侠",35),new StudentSort("周帅比",20),new StudentSort("陈董",21)};

        StudentSort[] students=new StudentSort[3];
        students[0]=new StudentSort("游大侠",35);
        students[1]=new StudentSort("周帅比",20);
        students[2]=new StudentSort("陈董",21);

        System.out.println(Arrays.toString(students));
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
}